<?php

/*
 * C订单作功能模块
 * 功能：发型师功能操作页
 */

namespace User\Controller;

use Think\C_Controller;

class BarberController extends C_Controller  {
    
    CONST ROUGH_STATUS = 2;
    CONST RELEASE_STATUS = 1;
    CONST DELETE_STATUS=4;
    
    public function __construct() {
        parent::__construct();
        if($_SESSION['barber_store']>0){
            $s = new \Home\Logic\StoreLogic();
            $store = $s->getStore($_SESSION['barber_store']);
            $this->assign('logo',$store['logo']);
            $this->assign('store_name',$store['name']);
        }
    }
    
    
    public function login(){
        layout(false);
        $this->is_login();
        $_SESSION['s_verify'] = time();
        $this->setSession($_SESSION);
        $this->display('login');
    }
    
    public function sublogin(){
        $this->is_login();
        $account = trim($_POST['account']);
        $code = trim($_POST['code']);
        $b = new \Home\Logic\BarberLogic();
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        if(!$m->checkCode($account, $code)){
            redirect('/user/barber/login?msg=1');
        }
        $user = $b->getBarberByMobile($account);
        if($user['id']<1){
            redirect('/user/barber/login?msg=1');
        }
        $_SESSION['barber_store'] = $user['store_id'];
        $_SESSION['barber_id'] = $user['id'];
        $this->setSession($_SESSION);
        redirect(b_buildUrl('/user/action'));
    }
    
    public function action(){
        if($_SESSION['barber_id']<1){
            $this->redirect(b_buildUrl('/user/action'));
        }
        $a = new \Home\Logic\ActionLogic();
        $actions = $a->getActionList($_SESSION['barber_store'],$_SESSION['barber_id'],0);
        $o = new \Home\Logic\OrderLogic();
        foreach($actions as $k=>$v){
            $re = $o->getActionOrderNumByActon($v['id']);
            $actions[$k]['num'] = (int)$re['num'];
            if($v['status']==self::ROUGH_STATUS){
                $actions[$k]['status_name'] = '草稿';
            }else{
                $time = time();
                $actions[$k]['status_name'] = $v['end_time']>$time?'进行中':'结束';
            }
        }
        $this->assign('actions',$actions);
        $this->display('action');
    }
    
    public function appendaction(){
        $a = new \Home\Logic\ActionLogic();
        $start = (int)$_GET['start'];
        $actions = $a->getActionList($_SESSION['barber_store'],$_SESSION['barber_id'],$start);
        if(count($actions)<1){
            exit('false');
        }else{
            $o = new \Home\Logic\OrderLogic();
            foreach($actions as $k=>$v){
            $re = $o->getActionOrderNumByActon($v['id']);
            $actions[$k]['num'] = (int)$re['num'];
                if($v['status']==self::ROUGH_STATUS){
                    $actions[$k]['status_name'] = '草稿';
                }else{
                    $time = time();
                    $actions[$k]['status_name'] = $v['end_time']>$time?'进行中':'结束';
                }
            }
            exit(json_encode($actions));
        }
    }
    
    /*暂时废弃*/
    public function info(){
        $this->is_login();
        if($this->barber['id']<1){
            $this->redirect('/user/barber/login');
        }
        if($this->barber['is_repassword']!=1){
            $this->redirect('/user/barber/repassword');
        }
        $a = new \Home\Logic\ActionLogic();
        $action = $a->getActionById((int)$_GET['aid']);
        if($action['store_id']!=$_SESSION['barber_store']){
            exit;
        }
        $s = new \Home\Logic\StoreLogic();
        $store = $s->getStore($_SESSION['barber_store']);
        $this->assign('store',$store);
        $this->assign('action',$action);
        $this->display('preview');
    }
    
    /*发短信*/
    public function code(){
        $mobile = trim($_GET['mobile']);
        if(!is_mobile($mobile)){
            exit('非法手机号');
        }
        import('MsgCode','Msgcode');
        $m = new \Msgcode\MsgCode();
        $re = $m->sendMsg_b($mobile);
        if($re){
            echo "true";
            exit;
        }
        exit('发送失败');
    }
    
    public function checkcode(){
        $code = trim($_GET['code']);
        $mobile = trim($_GET['mobile']);
        $b = new \Home\Logic\BarberLogic();
        $this->foo(json_encode($_SESSION).'---'.$code.'--'.$mobile);
        if($code != $_SESSION['msg_code'] || $mobile!=$_SESSION['msg_mobile']){
            exit('false');
        }
        exit('true');
    }
    
    protected function is_login(){
        if($_SESSION['barber_id']>1){
            redirect(b_buildUrl('/user/action'));
        }
    }
public function foo($str) {
        $f = fopen('./public/upload/g.txt', 'a+');
        fwrite($f, $str . "\r\n");
        fclose($f);
    }
    
}
